package com.gtug.hack.herbert_bangdao.Services;

import com.gtug.hack.herbert_bangdao.R;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.os.Vibrator;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class MonitorService extends Service {
	
	private static final String TAG  = "AppTag";
	private TelephonyManager tm;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override  
    public void onCreate() {  
		toast("service created");
        Log.e(TAG, "start onCreate~~~");  
        super.onCreate();  
    }  
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId)
	{
		Log.e(TAG, "start onStart~~~"); 
		super.onStart(intent, startId);   
        
        startSignalLevelListener();
		return START_STICKY;
	}
      
	/*
    @Override  
    public void onStart(Intent intent, int startId) {  
        Log.e(TAG, "start onStart~~~");  
        super.onStart(intent, startId);   
        
        startSignalLevelListener();
    } 
    */ 
      
    private void startSignalLevelListener() {
        tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    	tm.listen(phoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
		
	}
    
    private final PhoneStateListener phoneStateListener = new PhoneStateListener(){
    	@Override
    	public void onSignalStrengthsChanged(SignalStrength signalStrength)
        {
                Log.i(TAG, "onSignalStrengthChanged " 
                		+ " getCdmaDbm "  			+ signalStrength.getCdmaDbm() 
                		+ " getCdmaEcio " 			+ signalStrength.getCdmaEcio() 
                		+ " getEvdoDbm "  			+ signalStrength.getEvdoDbm()
                		+ " getEvdoEcio "			+ signalStrength.getEvdoEcio()
                		+ " getEvdoSnr " 			+ signalStrength.getEvdoSnr()
                		+ " getGsmSignalStrength "  + signalStrength.getGsmSignalStrength()
                		+ " isGsm " 				+ signalStrength.isGsm());
                
                
                if(isPoorSignal(signalStrength.getGsmSignalStrength()))
                {
                	//toast("bad signal");
                	notifyMsg();      	
                }
               // setSignalLevel(info_ids[INFO_SIGNAL_LEVEL_INDEX],info_ids[INFO_SIGNAL_LEVEL_INFO_INDEX],asu);
                
                super.onSignalStrengthsChanged(signalStrength);
        }
    };
    
    private boolean isPoorSignal(int signalVal)
    {
    	int dbmVal = -113 + signalVal*2; //calculate the corresponding dBm value
    	 Log.i(TAG,"Current dbm is " + dbmVal);
    	return dbmVal < -100; // if dBm value is less than -100, it is a bad signal
    }

	protected void notifyMsg() {
		final int HELLO_ID = 1;
		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
		int icon = R.drawable.ic_launcher;
		CharSequence tickerText = "Poor 3G signal alert";
		long when = System.currentTimeMillis();
		Notification notification = new Notification(icon, tickerText, when);
		CharSequence contentTitle = "Poor 3G signal observed";
		CharSequence contentText = "Click to change Radio settings";
		Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
		notificationIntent.setClassName("com.android.phone", "com.android.phone.Settings");
		
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
		
		notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
		
		mNotificationManager.notify(HELLO_ID, notification);
		
		// Get instance of Vibrator from current Context
		Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		 
		// Vibrate for 300 milliseconds
		v.vibrate(300);
	}

	@Override  
    public void onDestroy() {  
        Log.e(TAG, "start onDestroy~~~");  
        //toast("destroying");
        super.onDestroy();  
        tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
    }  
      
      
    @Override  
    public boolean onUnbind(Intent intent) {  
        Log.e(TAG, "start onUnbind~~~");  
        return super.onUnbind(intent);  
    }  
    
    
    private void toast(String msg)
    {
    	Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
    }

}


